package com.senven.api.controller;

import com.senven.api.annotation.LogInfo;
import com.senven.api.commom.ResponseCode;
import com.senven.api.commom.ResponsePage;
import com.senven.api.commom.ResponseResult;
import com.senven.api.dao.OrganMapper;
import com.senven.api.exception.GlobalException;
import com.senven.api.pojo.Organ;
import com.senven.api.service.OrganService;

import com.senven.api.utils.ReponseResultUtil;
import org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.xml.transform.Result;
import java.util.List;

/**
 * @author LQP
 */
@RequestMapping("/api/organ")
@Controller
public class OrganController {

    @Autowired
    private OrganService organService;

    //@LogInfo(operateInfo = "查询社团列表")
    @GetMapping("/queryOrganList")
    @ResponseBody
    public ResponsePage<Organ>getOrgan(@RequestParam(value = "current", defaultValue = "1") int current,
                                         @RequestParam(value = "size",defaultValue = "10")int size){
        return organService.queryOrganList(current,size);
    }


    /**
     * ID查询社团信息
     * 路径中获取参数
     * @param id
     * @return
     */
    @GetMapping(value = "/queryOrganListById")
    @ResponseBody
    public Organ queryOrganListById(@RequestParam("id") int id){
       return organService.queryOrganListById(id);
    }

    /**
     * 根据社团名进行查询
     * @param name
     * @return
     */
    @GetMapping(value = "/queryOrganListByName")
    @ResponseBody
    public ResponsePage<Organ> queryOrganListByName(@RequestParam("name") String name,
                                                    @RequestParam(value = "current", defaultValue = "1") int current,
                                                    @RequestParam(value = "size",defaultValue = "10")int size){
        return organService.queryOrganListByName(name,current,size);
    }

    /**
     * 插入一个社团
     * @param organ
     */
    @RequestMapping(value = "/insertOrgan",method = RequestMethod.POST)
    @ResponseBody
    @LogInfo(operateType = "社团业务",operateInfo = "插入一个社团")
    public ResponseResult insertOrgan(@RequestBody Organ organ){
        try {

            ResponsePage<Organ> responsePage= new ResponsePage<Organ>();
            responsePage = organService.queryOrganListByNameOne(organ.getName(),1,10);
            if (!responsePage.getData().isEmpty() || responsePage.getData().size() != 0){
                return ReponseResultUtil.error(-1, "请勿重复插入");
            }

            int result;
            result = organService.insertOrgan(organ);
            System.out.println(result);
            if (result == 0) {
                return ReponseResultUtil.error(-1, "社团插入失败");
            } else {
                return ReponseResultUtil.success("社团插入成功");
            }
        }catch (Exception e){
            throw  new GlobalException(ResponseCode.ERROR.getCode(),"数据库错误");
        }
    }

    /**
     * 更新一个社团
     * @param organ
     */
    @RequestMapping(value = "/updateOrgan",method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult updateOrgan(@RequestBody Organ organ){
        //System.out.println(organ.getName());
        try {
            int result = organService.updateOrgan(organ);
            if (result == 0) {
                return ReponseResultUtil.error(-1, "社团更新失败");
            } else {
                return ReponseResultUtil.success("社团更新成功");
            }
        }catch (Exception e){
            throw new GlobalException(ResponseCode.ERROR.getCode(),"数据库错误");
        }
    }

    /**
     * 删除一个社团
     * @param id
     */
    @GetMapping("/deleteOrgan")
    @ResponseBody
    public ResponseResult deleteOrgan(@RequestParam("id") int id){
        //System.out.println(organ.getName());
        try {
            int result = organService.deleteOrgan(id);
            if (result == 0) {
                return ReponseResultUtil.error(-1, "社团删除失败");
            } else {
                return ReponseResultUtil.success("社团删除成功");
            }
        }catch (Exception e){
            throw new GlobalException(ResponseCode.ERROR.getCode(),"数据库错误");
        }
    }

}
